import { ValidationChain, validationResult } from "express-validator";
import { Request, Response

  
 } from "express";
import { responseError } from "../common/response";

const validate = async (validations: ValidationChain[], req: Request, res: Response): Promise<boolean> => {
  for (const validation of validations) {
    await validation.run(req);
  }

  // 获取验证结果
  const result = validationResult(req);
  // 如果验证结果为空，直接跳过
  if (result.isEmpty()) return true;
  // 获取验证结果数组
  const array = result.array();
  console.log(array);
  // 获取验证结果信息
  const message = array.map(item => item.msg).toString();
  // 返回验证错误信息
  res.json(responseError(message));
  return false;
};

export default validate;
